const {
  TeaModel,
  StuModel,
  jobAdnMenuModel,
  jobAndBtnModel,
} = require("../model");
const { createToken } = require("../utiles/jwt");
exports.login = async (req, res) => {
  let { name, password, status } = req.body;
  if (name === "" || password === "" || status === "") {
    return res.status(400).json({
      code: 400,
      data: null,
      message: "请输入账号或者密码或者状态",
    });
  }

  if (status === 1) {
    let isTrue = await StuModel.findOne({ stuNum: name });
    if (!isTrue) {
      return res.status(201).json({
        code: 201,
        data: null,
        message: "该账号不存在",
      });
    }
    try {
      let result = await StuModel.findOne({
        stuNum: name,
        password: password,
      }).populate("jobId", "jobName");
      if (result) {
        let menusArr = [];
        let btnsArr = [];
        let jobId = result.jobId._id;
        let menus = await jobAdnMenuModel
          .find({ jobId: jobId })
          .populate("MenuId", "menuName menuValue");
        menus.forEach((item) => {
          let obj = {
            menuName: item.MenuId.menuName,
            menuValue: item.MenuId.menuValue,
          };
          menusArr.push(obj);
        });
        let btns = await jobAndBtnModel
          .find({ jobId: jobId })
          .populate("btnId", "ButtonName ButtonValue");
        btns.forEach((item) => {
          let obj = {
            ButtonName: item.btnId.ButtonName,
            ButtonValue: item.btnId.ButtonValue,
          };
          btnsArr.push(obj);
        });
        let token = await createToken(result._doc);

        delete result._doc.password;
        result._doc.menusArr = menusArr;
        result._doc.btnsArr = btnsArr;
        return res.status(200).json({
          code: 200,
          data: {
            info: result,
            token: token,
          },
          message: "登录成功",
        });
      } else {
        return res.status(201).json({
          code: 201,
          data: null,
          message: "账号或者密码错误",
        });
      }
    } catch (error) {
      return res.status(500).json({
        code: 500,
        data: null,
        message: "服务器出现问题，请稍后尝试",
      });
    }
  } else if (status === 2) {
    let isTrue = await TeaModel.findOne({ teaNumber: name });
    if (!isTrue) {
      return res.status(201).json({
        code: 201,
        data: null,
        message: "该账号不存在",
      });
    }
    //老师登录逻辑
    try {
      let result = await TeaModel.findOne({
        teaNumber: name,
        teaPassword: password,
      }).populate("jobId", "jobName");
      let menusArr = [];
      let btnsArr = [];
      let jobId = result.jobId._id;
      let menus = await jobAdnMenuModel
        .find({ jobId: jobId })
        .populate("MenuId", "menuName menuValue");
      menus.forEach((item) => {
        let obj = {
          menuName: item.MenuId.menuName,
          menuValue: item.MenuId.menuValue,
        };
        menusArr.push(obj);
      });
      let btns = await jobAndBtnModel
        .find({ jobId: jobId })
        .populate("btnId", "ButtonName ButtonValue");
      btns.forEach((item) => {
        let obj = {
          ButtonName: item.btnId.ButtonName,
          ButtonValue: item.btnId.ButtonValue,
        };
        btnsArr.push(obj);
      });
      if (result) {
        let token = await createToken(result._doc);
        delete result._doc.teaPassword;
        result._doc.menusArr = menusArr;
        result._doc.btnsArr = btnsArr;
        res.status(200).json({
          code: 200,
          data: {
            info: result,
            token: token,
          },
          message: "登录成功",
        });
      } else {
        res.status(201).json({
          code: 201,
          data: null,
          message: "账号或者密码错误",
        });
      }
    } catch (error) {
      res.status(500).json({
        code: 500,
        data: null,
        message: "服务器出现问题，请稍后尝试",
      });
    }
  } else {
    return res.status(400).json({
      code: 400,
      data: null,
      message: "请输入正确的登录状态",
    });
  }
};
//修改密码
exports.changePassword = async (req, res) => {
  let { password } = req.body;
  if (!password) {
    return res.status(400).json({
      code: 400,
      data: null,
      message: "请输入密码",
    });
  }
  if (!req.user) {
    return res.status(400).json({
      code: 400,
      data: null,
      message: "请先登录",
    });
  }
  try {
    if (req.user.stuName) {
      //修改学生
      await StuModel.findByIdAndUpdate(req.user._id, {
        password: password,
      });
      return res.status(200).json({
        code: 200,
        data: null,
        message: "修改成功",
      });
    } else {
      //修改教师
      await TeaModel.findByIdAndUpdate(req.user._id, {
        teaPassword: password,
      });
      return res.status(200).json({
        code: 200,
        data: null,
        message: "修改成功",
      });
    }
  } catch (error) {
    return res.status(500).json({
      code: 500,
      data: null,
      message: "服务器出现问题，请稍后尝试",
    });
  }
};
//获取用户信息
exports.getUserInfo = async (req, res) => {
  if (!req.user) {
    return res.status(400).json({
      code: 400,
      data: null,
      message: "请先登录",
    });
  }
  try {
    if (req.user.stuName) {
      //学生
      let result = await StuModel.findById(req.user._id).populate(
        "jobId",
        "jobName"
      );
      if (!result) {
        return res.status(400).json({
          code: 400,
          data: null,
          message: "用户不存在",
        });
      }
      let menusArr = [];
      let btnsArr = [];
      let jobId = result.jobId._id;
      let menus = await jobAdnMenuModel
        .find({ jobId: jobId })
        .populate("MenuId", "menuName menuValue");
      menus.forEach((item) => {
        let obj = {
          menuName: item.MenuId.menuName,
          menuValue: item.MenuId.menuValue,
        };
        menusArr.push(obj);
      });
      let btns = await jobAndBtnModel
        .find({ jobId: jobId })
        .populate("btnId", "ButtonName ButtonValue");
      btns.forEach((item) => {
        let obj = {
          ButtonName: item.btnId.ButtonName,
          ButtonValue: item.btnId.ButtonValue,
        };
        btnsArr.push(obj);
      });
      result._doc.menusArr = menusArr;
      result._doc.btnsArr = btnsArr;
      return res.status(200).json({
        code: 200,
        data: result,
        message: "获取成功",
      });
    } else {
      //教师
      let result = await TeaModel.findById(req.user._id).populate(
        "jobId",
        "jobName"
      );
      if (!result) {
        return res.status(400).json({
          code: 400,
          data: null,
          message: "用户不存在",
        });
      }
      let menusArr = [];
      let btnsArr = [];
      let jobId = result.jobId._id;
      let menus = await jobAdnMenuModel
        .find({ jobId: jobId })
        .populate("MenuId", "menuName menuValue");
      menus.forEach((item) => {
        let obj = {
          menuName: item.MenuId.menuName,
          menuValue: item.MenuId.menuValue,
        };
        menusArr.push(obj);
      });
      let btns = await jobAndBtnModel
        .find({ jobId: jobId })
        .populate("btnId", "ButtonName ButtonValue");
      btns.forEach((item) => {
        let obj = {
          ButtonName: item.btnId.ButtonName,
          ButtonValue: item.btnId.ButtonValue,
        };
        btnsArr.push(obj);
      });
      result._doc.menusArr = menusArr;
      result._doc.btnsArr = btnsArr;
      return res.status(200).json({
        code: 200,
        data: result,
        message: "获取成功",
      });
    }
  } catch (error) {
    return res.status(500).json({
      code: 500,
      data: null,
      message: "服务器错误",
    });
  }
};
